<?php

namespace app\apiadmin\service\data;

use think\facade\Db;

class CommStaticDictService
{
    private static $dict = [
        "pay_ment" => [
            "1" => "支付宝",
            "2" => "微信",
            "10" => "抖音支付",
        ],
        "order_status" => [
            //  ，0 未支付，1正常单；2退单中，3，退单成功；4：退单失败

            "0" => "未支付",
            "1" => "正常单",
            "2" => "退单中",
            "3" => "退单成功",
            "4" => "退单失败",
        ],

        "pay_state" => [
            "1" => "已支付",
            "0" => "未支付",
        ],

        "refund_status" => [
            "0" => "等待审核",
            "1" => "正常",
            "2" => "审核中",
            "3" => "同意退款",
            "4" => "拒绝退款",
        ],
        "course_state" => [
            "1" => "上架中",
            "2" => "下架",
        ],
        "isfinished" => [
            '0' => '未领课',
            "1" => '已领课',
        ],
        // "dy_state" => [
        //     ['id'=>1,'name'=>'未审核'],
        //     ['id'=>2,'name'=>'审核中'],
        //     ['id'=>3,'name'=>'已通过审核'],
        //     ['id'=>4,'name'=>'未通过审核'],
        // ],
        "dy_state" => [
            '1' => '未审核',
            '2' => '审核中',
            '3' => '已通过审核',
            '4' => '未通过审核',
        ],
        "chapter_state" => [
            "1" => "未上架",
            "2" => "已上架",
        ],
        "chapter_free" => [
            "0" => "免费",
            "1" => "收费",
        ],
        "file_type" => [
            "1" => "视频",
            "2" => "音频",
        ],
        "file_free" => [
            "1" => "收费",
            "2" => "免费",
        ],
        "file_state" => [
            "1" => "上架",
            "0" => "下架",
        ],
        "sex" => [
            "0" => "未知",
            "1" => "男",
            "2" => "女",
        ],
        "user_type" => [
            "0" => "未知",
            "1" => "微信",
            "2" => "企业微信",
        ],
        "company_type" => [
            "0" => "内部",
            "1" => "服务商",
        ],
        "role" => [
            "1" => "超级管理员",
            "2" => "企业管理",
            "3" => "普通员工",
        ],
        "return_visit_status" => [
            "0" => "未沟通",
            "1" => "沟通中",
            "2" => "沟通完成",
        ],

        "pay_method" => [
            "1" => "微信",
            "2" => "支付宝",
            "3" => "其他",
        ],
        "state" => [
            //  ，0 未支付，1正常单；2退单中，3，退单成功；4：退单失败

            "0" => "未支付",
            "1" => "已支付",
            "2" => "退单中",
            "3" => "退单成功",
            "4" => "退款异常",
            "5" => "订单已取消",
            "6" => "退款关闭",
        ],
        "selledstate" => [
            //  ，0 未支付，1正常单；2退单中，3，退单成功；4：退单失败

            "1" => "退款成功",
            "2" => "退款关闭",
            "3" => "退款中",
            "4" => "退单异常",
        ],
        "order_from" => [
            //  ，0 未支付，1正常单；2退单中，3，退单成功；4：退单失败

            "1" => "直播",
            "2" => "分享",
            "0" => "其他",
        ],
        "from" => [
            "1" => "课程购买链接下单",
            "0" => "直播间下单",
        ],

    ];

    public static function HasDict($name)
    {

        return !empty(self::$dict[$name]);
    }

    public static function GetDict($name)
    {
        return !empty(self::$dict[$name]) ? self::toFrontDict(self::$dict[$name]) : [];
    }

    public static function FormatWithDict($name, $id, $defaultValue = "未知")
    {
        if (self::HasDict($name)) {
            return !empty(self::$dict[$name][$id]) ? self::$dict[$name][$id] : $defaultValue;
        }
    }

    public static function toFrontDict($dict)
    {
        $ret = [];
        array_walk($dict, function (&$value, $key) use (&$ret) {

            $ret[] = [
                "id" => $key,
                "name" => $value,
            ];

        });
        return $ret;
    }

    public static function loadDict($name, $id_name = "id", $value_name = "name", $where = [])
    {

        $list = Db::table("xh_" . $name)
            ->where(["del" => 0])
            ->where($where)
            ->field($id_name . " as id," . $value_name . " as name")
            ->select();

        $list = $list->toArray();
        $ret = [];
        array_walk($list, function (&$item) use (&$ret) {
            $ret[] = [
                "id" => $item["id"],
                "name" => $item["name"],
            ];
        });

        return $ret;
    }

    public static function loadDict2($name, $id_name = "id", $value_name = "name", $where = [])
    {

        $list = Db::name($name)
            ->where(["del" => 0])
            ->where($where)
            ->field($id_name . " as id," . $value_name . " as name")
            ->select();

        $list = $list->toArray();
        $ret = [];
        array_walk($list, function (&$item) use (&$ret) {
            $ret[] = [
                "id" => $item["id"],
                "name" => $item["name"],
            ];
        });

        return $ret;
    }

}
